/*   1:    */ package org.intellij.plugins.relaxNG.convert;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import org.intellij.plugins.relaxNG.compact.RncFileType;
import org.intellij.plugins.relaxNG.validation.ValidateAction;

import java.io.File;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */ 
/*  21:    */ public class ConvertSchemaAction
/*  22:    */   extends AnAction
/*  23:    */ {
/*  24:    */   public void update(AnActionEvent paramAnActionEvent)
/*  25:    */   {
/*  26: 47 */     VirtualFile[] arrayOfVirtualFile = (VirtualFile[])paramAnActionEvent.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
/*  27: 48 */     Project localProject = (Project)paramAnActionEvent.getData(CommonDataKeys.PROJECT);
/*  28: 49 */     if ((arrayOfVirtualFile != null) && (localProject != null))
/*  29:    */     {
/*  30: 50 */       SchemaType localSchemaType = a(localProject, arrayOfVirtualFile);
/*  31: 51 */       paramAnActionEvent.getPresentation().setEnabled(localSchemaType != null);
/*  32: 52 */       if (localSchemaType == SchemaType.XML) {
/*  33: 53 */         paramAnActionEvent.getPresentation().setText("Generate Schema from XML file" + (arrayOfVirtualFile.length > 1 ? "s" : "") + "...");
/*  34:    */       } else {
/*  35: 55 */         paramAnActionEvent.getPresentation().setText("Convert Schema...");
/*  36:    */       }
/*  37:    */     }
/*  38:    */     else
/*  39:    */     {
/*  40: 58 */       paramAnActionEvent.getPresentation().setEnabled(false);
/*  41:    */     }
/*  42:    */   }
/*  43:    */   
/*  44:    */   private static SchemaType a(Project paramProject, VirtualFile... paramVarArgs)
/*  45:    */   {
/*  46: 63 */     if (paramVarArgs.length == 0) {
/*  47: 63 */       return null;
/*  48:    */     }
/*  49: 65 */     VirtualFile localVirtualFile = paramVarArgs[0];
/*  50: 66 */     FileType localFileType = localVirtualFile.getFileType();
/*  51: 67 */     if (localFileType == StdFileTypes.XML)
/*  52:    */     {
/*  53: 68 */       PsiFile localPsiFile = PsiManager.getInstance(paramProject).findFile(localVirtualFile);
/*  54:    */       Object localObject1;
/*  55: 69 */       if ((localPsiFile instanceof XmlFile))
/*  56:    */       {
/*  57: 70 */         localObject1 = ((XmlFile)localPsiFile).getDocument();
/*  58: 71 */         if ((localObject1 != null) && (((XmlDocument)localObject1).getRootTag() != null))
/*  59:    */         {
/*  60: 72 */           XmlTag localXmlTag = ((XmlDocument)localObject1).getRootTag();
/*  61: 73 */           assert (localXmlTag != null);
/*  62: 74 */           String str = localXmlTag.getNamespace();
/*  63: 75 */           if (("http://relaxng.org/ns/structure/1.0".equals(str)) && (paramVarArgs.length == 1)) {
/*  64: 76 */             return SchemaType.RNG;
/*  65:    */           }
/*  66:    */         }
/*  67:    */       }
/*  68: 80 */       if (paramVarArgs.length > 1) {
/*  69: 81 */         for (Object localObject2 : paramVarArgs) {
/*  70: 82 */           if (localObject2.getFileType() == StdFileTypes.XML)
/*  71:    */           {
/*  72: 82 */             if (a(paramProject, new VirtualFile[] { localObject2 }) == null) {}
/*  73:    */           }
/*  74:    */           else {
/*  75: 83 */             return null;
/*  76:    */           }
/*  77:    */         }
/*  78:    */       }
/*  79: 87 */       return SchemaType.XML;
/*  80:    */     }
/*  81: 88 */     if ((localFileType == StdFileTypes.DTD) && (paramVarArgs.length == 1)) {
/*  82: 89 */       return SchemaType.DTD;
/*  83:    */     }
/*  84: 90 */     if ((localFileType == RncFileType.getInstance()) && (paramVarArgs.length == 1)) {
/*  85: 91 */       return SchemaType.RNC;
/*  86:    */     }
/*  87: 93 */     return null;
/*  88:    */   }
/*  89:    */   
/*  90:    */   public void actionPerformed(AnActionEvent paramAnActionEvent)
/*  91:    */   {
/*  92: 97 */     VirtualFile localVirtualFile1 = (VirtualFile)paramAnActionEvent.getData(CommonDataKeys.VIRTUAL_FILE);
/*  93: 98 */     Project localProject = (Project)paramAnActionEvent.getData(CommonDataKeys.PROJECT);
/*  94: 99 */     if ((localVirtualFile1 != null) && (localProject != null))
/*  95:    */     {
/*  96:100 */       VirtualFile[] arrayOfVirtualFile = (VirtualFile[])paramAnActionEvent.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
/*  97:101 */       assert (arrayOfVirtualFile != null);
/*  98:    */       
/*  99:103 */       SchemaType localSchemaType = a(localProject, arrayOfVirtualFile);
/* 100:104 */       ConvertSchemaDialog localConvertSchemaDialog = new ConvertSchemaDialog(localProject, localSchemaType, localVirtualFile1);
/* 101:105 */       localConvertSchemaDialog.show();
/* 102:106 */       if (!localConvertSchemaDialog.isOK()) {
/* 103:107 */         return;
/* 104:    */       }
/* 105:110 */       ValidateAction.saveFiles(arrayOfVirtualFile);
/* 106:    */       
/* 107:112 */       ConvertSchemaSettings localConvertSchemaSettings = localConvertSchemaDialog.getSettings();
/* 108:113 */       IdeaErrorHandler localIdeaErrorHandler = new IdeaErrorHandler(localProject);
/* 109:114 */       new IdeaDriver(localConvertSchemaSettings, localProject).convert(localSchemaType, localIdeaErrorHandler, arrayOfVirtualFile);
/* 110:    */       
/* 111:116 */       VirtualFile localVirtualFile2 = LocalFileSystem.getInstance().findFileByIoFile(new File(localConvertSchemaSettings.getOutputDestination()));
/* 112:117 */       if (localVirtualFile2 != null) {
/* 113:118 */         localVirtualFile2.refresh(false, true);
/* 114:    */       }
/* 115:    */     }
/* 116:    */   }
/* 117:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.convert.ConvertSchemaAction

 * JD-Core Version:    0.7.0.1

 */